package com.gsoe.judge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gsoe.base.model.MqMessage;
import com.gsoe.dto.SaveJudgeTestDTO;
import com.gsoe.judge.mapper.JudgeTestMapper;

import com.gsoe.judge.service.JudgeTestService;
import com.gsoe.po.JudgeTest;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 测验评测表 服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2024-11-04
 */
@Service
public class JudgeTestServiceImpl extends ServiceImpl<JudgeTestMapper, JudgeTest> implements JudgeTestService {


    @Override
    public void saveJudgeTest(SaveJudgeTestDTO saveJudgeTestDTO) {
        Map<Long,Integer> problemIdWithScore=saveJudgeTestDTO.getProblemIdWithScore();
        Map<Long,String>problemIdWithInput=saveJudgeTestDTO.getProblemIdWithInput();
        Long loginId= saveJudgeTestDTO.getLoginId();
        String loginName= saveJudgeTestDTO.getLoginName();
        Long testId= saveJudgeTestDTO.getTestId();

        remove(new QueryWrapper<JudgeTest>()
                .eq("user_id",loginId)
                .eq("test_id",testId)
        );

        List<JudgeTest>judgeTestList=new ArrayList<>();
        problemIdWithScore.forEach((k,v)->{
            JudgeTest judgeTest=new JudgeTest();
            judgeTest.setTestId(testId);
            judgeTest.setProblemId(k);
            judgeTest.setScore(v);
            judgeTest.setInput(problemIdWithInput.get(k));
            judgeTest.setUserId(loginId);
            judgeTest.setUserName(loginName);
            judgeTestList.add(judgeTest);
        });
        saveBatch(judgeTestList);
    }
}
